package com.be.beadmin.basicData.controller;


import com.be.beadmin.basicData.dto.input.UserDto;
import com.be.beadmin.basicData.dto.input.UserOrgFilter;
import com.be.beadmin.basicData.dto.input.UserRolesDto;
import com.be.beadmin.basicData.dto.output.UserNameId;
import com.be.beadmin.basicData.entity.User;
import com.be.beadmin.basicData.service.UserService;
import com.be.beadmin.common.entity.IdDto;
import com.be.beadmin.common.entity.PageEntity;

import com.be.beadmin.common.utils.ResultModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
@Api(tags = "系统权限模块")
@RestController
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 用户个人信息
     * @param request
     * @return
     */
    @ApiOperation("获取个人基本信息")
    @PostMapping(value = "/user/viewBaseInfo")
    public ResultModel viewBaseInfo(HttpServletRequest request) {
        return  ResultModel.success(userService.viewBaseInfo(request)) ;
    }

    /**
     * 获取当前用户的角色
     * @param request
     * @return
     */
    @ApiOperation("获取当前用户的角色")
    @PostMapping(value = "/user/getRolesByCurrentUser")
    public ResultModel getRolesByCurrentUser(HttpServletRequest request) {
        return  ResultModel.success(userService.getRolesByCurrentUser(request)) ;
    }

    /**
     * 用户信息
     * @param pageEntity
     * @return
     */
    @ApiOperation("获取用户分页信息")
    @PostMapping(value = "/system/getUserByPage")
    public ResultModel getUserByPage(@RequestBody UserOrgFilter pageEntity) {
        return  ResultModel.success(userService.getUserByPage(pageEntity)) ;
    }

    /**
     * 添加用户
     * @param userDto
     * @return
     */
    @ApiOperation("增加用户")
    @PostMapping("/system/addUser")
    public ResultModel addUser(@RequestBody UserDto userDto){
        userService.addUser(userDto);
        return ResultModel.success("添加成功");
    }

    /**
     * 删除用户
     * @param userDto
     * @return
     */
    @ApiOperation("删除用户")
    @PostMapping("/system/delUser")
    public ResultModel delUser(@RequestBody UserDto userDto){
        userService.delUser(userDto);
        return ResultModel.success("删除成功");
    }

    @ApiOperation("锁住用户")
    @PostMapping("/system/lockUser")
    public ResultModel lockUser(@RequestBody UserDto userDto){

        userService.lockUser(userDto);
        return ResultModel.success("锁定成功");
    }

    /**
     * 更新用户
     * @param userDto
     * @return
     */
    @ApiOperation("更新用户")
    @PostMapping("/system/updateUser")
    public ResultModel updateUser(@RequestBody UserDto userDto){
        userService.updateUser(userDto);
        return ResultModel.success("更新成功");
    }

    /**
     *
     * @param userDto
     * @return
     */
    @ApiOperation("修改用户密码")
    @PostMapping("/system/updatePassword")
    public ResultModel updatePassword(@RequestBody UserDto userDto){
        userService.updatePassword(userDto);
        return ResultModel.success("修改密码成功");
    }

    /**
     * 更新角色成功
     * @param userRolesDto
     * @return
     */
    @ApiOperation("根据用户ID保存角色")
    @PostMapping("/system/saveRolesByUserId")
    public ResultModel saveRoles(@RequestBody UserRolesDto userRolesDto){
        userService.saveRoles(userRolesDto);
        return ResultModel.success("更新角色成功");
    }


    /**
     * 根据用户获取角色信息
     * @param idDto
     * @return
     */
    @ApiOperation("加载相关角色信息")
    @PostMapping(value = "/system/loadTransferUserRelRole")
    public ResultModel ajaxLoadTransferUserRelRole(@RequestBody IdDto idDto) {
            Map<String,Object> map = userService.ajaxLoadTransferUserRelRole(idDto.getId());
            return ResultModel.success(map);
    }



    @ApiOperation("管理员直接登录系统")
    @PostMapping(value = "/system/reLoin")
    public ResultModel reLogin(@RequestBody UserDto userDto) {
        String token = userService.reLogin(userDto.getName());
        return ResultModel.success(token);
    }
    @ApiOperation("根据角色类型获取该角色的所有用户")
    @PostMapping("/project/getUserByRoleType")
    public ResultModel getUserByRoleType(@RequestParam String RoleName){
        List<User> userLists = userService.getUserByRoleType(RoleName);
        return ResultModel.success(userLists);
    }

    @ApiOperation("根据角色类型获取该角色的所有用户id和name")
    @PostMapping("/project/getUserNameAndIdByRoleType")
    public ResultModel getUserNameAndIdByRoleType(@RequestParam String RoleName){
        List<UserNameId> userLists = userService.getUserNameAndIdByRoleType(RoleName);
        return ResultModel.success(userLists);
    }

}
